using MaterialSkin.Controls;
using System;
using System.Drawing;
using System.Windows.Forms;
using VideoAutoGenerator.Core;

namespace VideoAutoGenerator.UI.UserControls
{
    public partial class BasicSettingsControl : MaterialSkin.Controls.MaterialCard
    {
        // 基本配置控件
        private MaterialLabel titleLabel;
        private MaterialTextBox titleMaterialTextBox;
        
        private MaterialLabel inputDirDatLabel;
        private MaterialTextBox inputDirDatMaterialTextBox;
        private MaterialButton inputDirDatButton;
        
        private MaterialLabel inputDirMp3Label;
        private MaterialTextBox inputDirMp3MaterialTextBox;
        private MaterialButton inputDirMp3Button;
        
        private MaterialLabel inputStartVoiceLabel;
        private MaterialTextBox inputStartVoiceMaterialTextBox;
        private MaterialButton inputStartVoiceButton;
        
        private MaterialLabel inputDirImagesLabel;
        private MaterialTextBox inputDirImagesMaterialTextBox;
        private MaterialButton inputDirImagesButton;
        
        private MaterialLabel inputDirVideosLabel;
        private MaterialTextBox inputDirVideosMaterialTextBox;
        private MaterialButton inputDirVideosButton;
        
        private MaterialLabel outputDirSrtLabel;
        private MaterialTextBox outputDirSrtMaterialTextBox;
        private MaterialButton outputDirSrtButton;
        
        private MaterialLabel outputDirVideoLabel;
        private MaterialTextBox outputDirVideoMaterialTextBox;
        private MaterialButton outputDirVideoButton;
        
        private MaterialLabel mediaStoreLabel;
        private MaterialTextBox mediaStoreMaterialTextBox;
        private MaterialButton mediaStoreButton;
        
        private MaterialLabel pexelsApiKeyLabel;
        private MaterialTextBox pexelsApiKeyMaterialTextBox;
        
        private MaterialLabel ffmpegPathLabel;
        private MaterialTextBox ffmpegPathMaterialTextBox;
        private MaterialButton ffmpegPathButton;
        
        private MaterialLabel encodeFormatLabel;
        private ComboBox encodeFormatComboBox;
        
        public BasicSettingsControl()
        {
            InitializeComponent();
        }
        
        private void InitializeComponent()
        {
            this.SuspendLayout();
            
            // 创建控件
            CreateControls();
            
            // 设置控件位置和样式
            SetupLayout();
            
            // 添加事件处理程序
            SetupEventHandlers();
            
            this.ResumeLayout(false);
        }
        
        private void CreateControls()
        {
            // 标题
            titleLabel = new MaterialLabel();
            titleMaterialTextBox = new MaterialTextBox();
            
            // DAT输入目录
            inputDirDatLabel = new MaterialLabel();
            inputDirDatMaterialTextBox = new MaterialTextBox();
            inputDirDatButton = new MaterialButton();
            
            // MP3输入目录
            inputDirMp3Label = new MaterialLabel();
            inputDirMp3MaterialTextBox = new MaterialTextBox();
            inputDirMp3Button = new MaterialButton();
            
            // 开始声音文件
            inputStartVoiceLabel = new MaterialLabel();
            inputStartVoiceMaterialTextBox = new MaterialTextBox();
            inputStartVoiceButton = new MaterialButton();
            
            // 图片输入目录
            inputDirImagesLabel = new MaterialLabel();
            inputDirImagesMaterialTextBox = new MaterialTextBox();
            inputDirImagesButton = new MaterialButton();
            
            // 视频输入目录
            inputDirVideosLabel = new MaterialLabel();
            inputDirVideosMaterialTextBox = new MaterialTextBox();
            inputDirVideosButton = new MaterialButton();
            
            // SRT输出目录
            outputDirSrtLabel = new MaterialLabel();
            outputDirSrtMaterialTextBox = new MaterialTextBox();
            outputDirSrtButton = new MaterialButton();
            
            // 视频输出目录
            outputDirVideoLabel = new MaterialLabel();
            outputDirVideoMaterialTextBox = new MaterialTextBox();
            outputDirVideoButton = new MaterialButton();
            
            // 媒体存储目录
            mediaStoreLabel = new MaterialLabel();
            mediaStoreMaterialTextBox = new MaterialTextBox();
            mediaStoreButton = new MaterialButton();
            
            // Pexels API密钥
            pexelsApiKeyLabel = new MaterialLabel();
            pexelsApiKeyMaterialTextBox = new MaterialTextBox();
            
            // FFmpeg路径
            ffmpegPathLabel = new MaterialLabel();
            ffmpegPathMaterialTextBox = new MaterialTextBox();
            ffmpegPathButton = new MaterialButton();
            
            // 编码格式
            encodeFormatLabel = new MaterialLabel();
            encodeFormatComboBox = new ComboBox();
        }
        
        private void SetupLayout()
        {
            // 设置控件属性
            this.AutoScroll = true;
            this.Dock = DockStyle.Fill;
            
            // 标题
            titleLabel.Text = "项目标题:";
            titleLabel.AutoSize = true;
            titleLabel.TextAlign = ContentAlignment.MiddleLeft;
            titleLabel.Margin = new Padding(0, 10, 0, 0);
            
            titleMaterialTextBox.Size = new Size(400, 30);
            
            // DAT输入目录
            inputDirDatLabel.Text = "DAT输入目录:";
            inputDirDatLabel.AutoSize = true;
            inputDirDatLabel.TextAlign = ContentAlignment.MiddleLeft;
            inputDirDatLabel.Margin = new Padding(0, 10, 0, 0);
            inputDirDatMaterialTextBox.ReadOnly = true;
            inputDirDatButton.Text = "浏览";
            inputDirDatButton.Width = 70;
            
            // MP3输入目录
            inputDirMp3Label.Text = "MP3输入目录:";
            inputDirMp3Label.AutoSize = true;
            inputDirMp3Label.TextAlign = ContentAlignment.MiddleLeft;
            inputDirMp3Label.Margin = new Padding(0, 10, 0, 0);
            inputDirMp3MaterialTextBox.ReadOnly = true;
            inputDirMp3Button.Text = "浏览";
            inputDirMp3Button.Width = 70;
            
            // 开始声音文件
            inputStartVoiceLabel.Text = "开始声音文件:";
            inputStartVoiceLabel.AutoSize = true;
            inputStartVoiceLabel.TextAlign = ContentAlignment.MiddleLeft;
            inputStartVoiceLabel.Margin = new Padding(0, 10, 0, 0);
            inputStartVoiceMaterialTextBox.ReadOnly = true;
            inputStartVoiceButton.Text = "浏览";
            inputStartVoiceButton.Width = 70;
            
            // 图片输入目录
            inputDirImagesLabel.Text = "图片输入目录:";
            inputDirImagesLabel.AutoSize = true;
            inputDirImagesLabel.TextAlign = ContentAlignment.MiddleLeft;
            inputDirImagesLabel.Margin = new Padding(0, 10, 0, 0);
            inputDirImagesMaterialTextBox.ReadOnly = true;
            inputDirImagesButton.Text = "浏览";
            inputDirImagesButton.Width = 70;
            
            // 视频输入目录
            inputDirVideosLabel.Text = "视频输入目录:";
            inputDirVideosLabel.AutoSize = true;
            inputDirVideosLabel.TextAlign = ContentAlignment.MiddleLeft;
            inputDirVideosLabel.Margin = new Padding(0, 10, 0, 0);
            inputDirVideosMaterialTextBox.ReadOnly = true;
            inputDirVideosButton.Text = "浏览";
            inputDirVideosButton.Width = 70;
            
            // SRT输出目录
            outputDirSrtLabel.Text = "SRT输出目录:";
            outputDirSrtLabel.AutoSize = true;
            outputDirSrtLabel.TextAlign = ContentAlignment.MiddleLeft;
            outputDirSrtLabel.Margin = new Padding(0, 10, 0, 0);
            outputDirSrtMaterialTextBox.ReadOnly = true;
            outputDirSrtButton.Text = "浏览";
            outputDirSrtButton.Width = 70;
            
            // 视频输出目录
            outputDirVideoLabel.Text = "视频输出目录:";
            outputDirVideoLabel.AutoSize = true;
            outputDirVideoLabel.TextAlign = ContentAlignment.MiddleLeft;
            outputDirVideoLabel.Margin = new Padding(0, 10, 0, 0);
            outputDirVideoMaterialTextBox.ReadOnly = true;
            outputDirVideoButton.Text = "浏览";
            outputDirVideoButton.Width = 70;
            
            // 媒体存储目录
            mediaStoreLabel.Text = "媒体存储目录:";
            mediaStoreLabel.AutoSize = true;
            mediaStoreLabel.TextAlign = ContentAlignment.MiddleLeft;
            mediaStoreLabel.Margin = new Padding(0, 10, 0, 0);
            mediaStoreMaterialTextBox.ReadOnly = true;
            mediaStoreButton.Text = "浏览";
            mediaStoreButton.Width = 70;
            
            // Pexels API密钥
            pexelsApiKeyLabel.Text = "Pexels API密钥:";
            pexelsApiKeyLabel.AutoSize = true;
            pexelsApiKeyLabel.TextAlign = ContentAlignment.MiddleLeft;
            pexelsApiKeyLabel.Margin = new Padding(0, 10, 0, 0);
            
            // FFmpeg路径
            ffmpegPathLabel.Text = "FFmpeg路径:";
            ffmpegPathLabel.AutoSize = true;
            ffmpegPathLabel.TextAlign = ContentAlignment.MiddleLeft;
            ffmpegPathLabel.Margin = new Padding(0, 10, 0, 0);
            ffmpegPathMaterialTextBox.ReadOnly = true;
            ffmpegPathButton.Text = "浏览";
            ffmpegPathButton.Width = 70;
            
            // 编码格式
            encodeFormatLabel.Text = "编码格式:";
            encodeFormatLabel.AutoSize = true;
            encodeFormatComboBox.Items.Add("h264_nvenc");
            encodeFormatComboBox.Items.Add("libx264");
            encodeFormatComboBox.SelectedIndex = 0;
            
            // 设置控件位置
            var layoutPanel = new MaterialSkin.Controls.MaterialLabel();
            layoutPanel.Dock = DockStyle.Fill;
            
            var tableLayout = new TableLayoutPanel();
            tableLayout.AutoScroll = true;
            tableLayout.Dock = DockStyle.Fill;
            tableLayout.ColumnCount = 3;
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 80F));
            tableLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20F));
            tableLayout.RowCount = 15;
            // 使用AutoSize使行高自适应内容
            for (int i = 0; i < 15; i++)
            {
                tableLayout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            }
            
            // 添加控件到表格布局
            tableLayout.Controls.Add(titleLabel, 0, 0);
            tableLayout.Controls.Add(titleMaterialTextBox, 1, 0);
            tableLayout.SetColumnSpan(titleMaterialTextBox, 2);
            
            tableLayout.Controls.Add(inputDirDatLabel, 0, 1);
            tableLayout.Controls.Add(inputDirDatMaterialTextBox, 1, 1);
            tableLayout.Controls.Add(inputDirDatButton, 2, 1);
            
            tableLayout.Controls.Add(inputDirMp3Label, 0, 2);
            tableLayout.Controls.Add(inputDirMp3MaterialTextBox, 1, 2);
            tableLayout.Controls.Add(inputDirMp3Button, 2, 2);
            
            tableLayout.Controls.Add(inputStartVoiceLabel, 0, 3);
            tableLayout.Controls.Add(inputStartVoiceMaterialTextBox, 1, 3);
            tableLayout.Controls.Add(inputStartVoiceButton, 2, 3);
            
            tableLayout.Controls.Add(inputDirImagesLabel, 0, 4);
            tableLayout.Controls.Add(inputDirImagesMaterialTextBox, 1, 4);
            tableLayout.Controls.Add(inputDirImagesButton, 2, 4);
            
            tableLayout.Controls.Add(inputDirVideosLabel, 0, 5);
            tableLayout.Controls.Add(inputDirVideosMaterialTextBox, 1, 5);
            tableLayout.Controls.Add(inputDirVideosButton, 2, 5);
            
            tableLayout.Controls.Add(outputDirSrtLabel, 0, 6);
            tableLayout.Controls.Add(outputDirSrtMaterialTextBox, 1, 6);
            tableLayout.Controls.Add(outputDirSrtButton, 2, 6);
            
            tableLayout.Controls.Add(outputDirVideoLabel, 0, 7);
            tableLayout.Controls.Add(outputDirVideoMaterialTextBox, 1, 7);
            tableLayout.Controls.Add(outputDirVideoButton, 2, 7);
            
            tableLayout.Controls.Add(mediaStoreLabel, 0, 8);
            tableLayout.Controls.Add(mediaStoreMaterialTextBox, 1, 8);
            tableLayout.Controls.Add(mediaStoreButton, 2, 8);
            
            tableLayout.Controls.Add(pexelsApiKeyLabel, 0, 9);
            tableLayout.Controls.Add(pexelsApiKeyMaterialTextBox, 1, 9);
            tableLayout.SetColumnSpan(pexelsApiKeyMaterialTextBox, 2);
            
            tableLayout.Controls.Add(ffmpegPathLabel, 0, 10);
            tableLayout.Controls.Add(ffmpegPathMaterialTextBox, 1, 10);
            tableLayout.Controls.Add(ffmpegPathButton, 2, 10);
            
            tableLayout.Controls.Add(encodeFormatLabel, 0, 11);
            tableLayout.Controls.Add(encodeFormatComboBox, 1, 11);
            tableLayout.SetColumnSpan(encodeFormatComboBox, 2);

            for(int i = 0;i < tableLayout.Controls.Count; i++)
            {
                var ctrl = tableLayout.Controls[i];
                if(ctrl is MaterialTextBox textBox)
                {
                    textBox.Width = 300;
                    // 添加锚定属性，使控件能够正确拉伸
                    textBox.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                }
                else if (ctrl is ComboBox comboBox)
                {
                    comboBox.Width = 300;
                    // 为ComboBox添加锚定属性
                    comboBox.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                }
                else if (ctrl is MaterialButton button)
                {
                    button.Anchor = AnchorStyles.Right;
                    button.Width = 70; // 统一按钮宽度
                }
            }
            
            layoutPanel.Controls.Add(tableLayout);
            this.Controls.Add(layoutPanel);
        }

        private void SetupEventHandlers()
        {
            // 为浏览按钮添加事件处理程序
            inputDirDatButton.Click += BrowseButton_Click;
            inputDirMp3Button.Click += BrowseButton_Click;
            inputStartVoiceButton.Click += BrowseButton_Click;
            inputDirImagesButton.Click += BrowseButton_Click;
            inputDirVideosButton.Click += BrowseButton_Click;
            outputDirSrtButton.Click += BrowseButton_Click;
            outputDirVideoButton.Click += BrowseButton_Click;
            mediaStoreButton.Click += BrowseButton_Click;
            ffmpegPathButton.Click += BrowseButton_Click;
        }
        
        private void BrowseButton_Click(object sender, EventArgs e)
        {
            MaterialButton button = sender as MaterialButton;
            if (button == inputDirDatButton)
            {
                BrowseForFolder(inputDirDatMaterialTextBox);
            }
            else if (button == inputDirMp3Button)
            {
                BrowseForFolder(inputDirMp3MaterialTextBox);
            }
            else if (button == inputStartVoiceButton)
            {
                BrowseForFile(inputStartVoiceMaterialTextBox, "WAV文件|*.wav");
            }
            else if (button == inputDirImagesButton)
            {
                BrowseForFolder(inputDirImagesMaterialTextBox);
            }
            else if (button == inputDirVideosButton)
            {
                BrowseForFolder(inputDirVideosMaterialTextBox);
            }
            else if (button == outputDirSrtButton)
            {
                BrowseForFolder(outputDirSrtMaterialTextBox);
            }
            else if (button == outputDirVideoButton)
            {
                BrowseForFolder(outputDirVideoMaterialTextBox);
            }
            else if (button == mediaStoreButton)
            {
                BrowseForFolder(mediaStoreMaterialTextBox);
            }
            else if (button == ffmpegPathButton)
            {
                BrowseForFile(ffmpegPathMaterialTextBox, "可执行文件|*.exe");
            }
        }
        
        private void BrowseForFolder(MaterialTextBox MaterialTextBox)
        {
            using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
            {
                if (folderDialog.ShowDialog() == DialogResult.OK)
                {
                    MaterialTextBox.Text = folderDialog.SelectedPath;
                }
            }
        }
        
        private void BrowseForFile(MaterialTextBox MaterialTextBox, string filter)
        {
            using (OpenFileDialog fileDialog = new OpenFileDialog())
            {
                fileDialog.Filter = filter;
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    MaterialTextBox.Text = fileDialog.FileName;
                }
            }
        }

        // 从配置管理器加载配置数据到控件
        public void LoadConfig()
        {
            var config = ConfigManager.Instance;
            
            titleMaterialTextBox.Text = config.GetString("title");
            inputDirDatMaterialTextBox.Text = config.GetString("inputDirDat");
            inputDirMp3MaterialTextBox.Text = config.GetString("inputDirMp3");
            inputStartVoiceMaterialTextBox.Text = config.GetString("inputStartVoice");
            inputDirImagesMaterialTextBox.Text = config.GetString("inputDirImages");
            inputDirVideosMaterialTextBox.Text = config.GetString("inputDirVideos");
            outputDirSrtMaterialTextBox.Text = config.GetString("outputDirSrt");
            outputDirVideoMaterialTextBox.Text = config.GetString("outputDirVideo");
            mediaStoreMaterialTextBox.Text = config.GetString("mediaStore");
            pexelsApiKeyMaterialTextBox.Text = config.GetString("pexelsApiKey");
            ffmpegPathMaterialTextBox.Text = config.GetObject("ffmpeg")["path"].ToString();
            
            var encodeFormat = config.GetString("encodeFormat");
            if (encodeFormatComboBox.Items.Contains(encodeFormat))
            {
                encodeFormatComboBox.SelectedItem = encodeFormat;
            }
            else
            {
                encodeFormatComboBox.SelectedIndex = 0;
            }
        }

        // 从控件保存配置数据到配置管理器
        public void SaveConfig()
        {
            var config = ConfigManager.Instance;
            
            config.SetValue("title", titleMaterialTextBox.Text);
            config.SetValue("inputDirDat", inputDirDatMaterialTextBox.Text);
            config.SetValue("inputDirMp3", inputDirMp3MaterialTextBox.Text);
            config.SetValue("inputStartVoice", inputStartVoiceMaterialTextBox.Text);
            config.SetValue("inputDirImages", inputDirImagesMaterialTextBox.Text);
            config.SetValue("inputDirVideos", inputDirVideosMaterialTextBox.Text);
            config.SetValue("outputDirSrt", outputDirSrtMaterialTextBox.Text);
            config.SetValue("outputDirVideo", outputDirVideoMaterialTextBox.Text);
            config.SetValue("mediaStore", mediaStoreMaterialTextBox.Text);
            config.SetValue("pexelsApiKey", pexelsApiKeyMaterialTextBox.Text);
            config.SetValue("ffmpeg.path", ffmpegPathMaterialTextBox.Text);
            
            if (encodeFormatComboBox.SelectedItem != null)
            {
                config.SetValue("encodeFormat", encodeFormatComboBox.SelectedItem.ToString());
            }
            
            config.SaveConfig();
        }
    }
}